#!/system/bin/sh
# Project WIPE https://github.com/yc9559/cpufreq-interactive-opt
# Author: yc9559
# Generated at: Tue Apr 24 22:51:24 2018

# $1:value $2:file path
function set_value() {
	if [ -f $2 ]; then
		chown 0.0 $2
		chmod 0666 $2
		echo $1 > $2
		chmod 0444 $2
	fi
}

# $1:cpu0 $2:timer_rate $3:value
function set_param() {
	echo $3 > /sys/devices/system/cpu/$1/cpufreq/interactive/$2
}

action=$1
setenforce 0

set_value 1 /sys/devices/system/cpu/cpu4/online
chown 0.0 /sys/devices/system/cpu/cpu0/cpufreq/interactive/*
chown 0.0 /sys/devices/system/cpu/cpu4/cpufreq/interactive/*
chmod 0666 /sys/devices/system/cpu/cpu0/cpufreq/interactive/*
chmod 0666 /sys/devices/system/cpu/cpu4/cpufreq/interactive/*

# RunOnce
if [ ! -f /project_wipe_runonce ]; then
	# set flag
	touch /project_wipe_runonce

	set_value 480000 /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq
	set_value 880000 /sys/devices/system/cpu/cpu4/cpufreq/scaling_min_freq

	# shared interactive parameters
	set_param cpu0 timer_rate 20000
	set_param cpu4 timer_rate 20000
	set_param cpu0 timer_slack 180000
	set_param cpu4 timer_slack 180000
	set_param cpu0 io_is_busy 0
	set_param cpu4 io_is_busy 0
fi

if [ "$action" = "powersave" ]; then
	set_param cpu0 above_hispeed_delay "18000 1680000:58000"
	set_param cpu0 hispeed_freq 1380000
	set_param cpu0 go_hispeed_load 99
	set_param cpu0 boostpulse_duration 18000
	set_param cpu0 target_loads "85 1380000:99"
	set_param cpu0 min_sample_time 18000
	set_param cpu4 above_hispeed_delay "18000 1780000:138000"
	set_param cpu4 hispeed_freq 1380000
	set_param cpu4 go_hispeed_load 95
	set_param cpu4 boostpulse_duration 18000
	set_param cpu4 target_loads "85 1380000:96"
	set_param cpu4 min_sample_time 18000
fi

if [ "$action" = "balance" ]; then
	set_param cpu0 above_hispeed_delay "18000 1380000:58000"
	set_param cpu0 hispeed_freq 980000
	set_param cpu0 go_hispeed_load 80
	set_param cpu0 boostpulse_duration 18000
	set_param cpu0 target_loads "85 980000:76 1380000:71 1680000:99"
	set_param cpu0 min_sample_time 18000
	set_param cpu4 above_hispeed_delay "18000 1780000:138000"
	set_param cpu4 hispeed_freq 1380000
	set_param cpu4 go_hispeed_load 82
	set_param cpu4 boostpulse_duration 38000
	set_param cpu4 target_loads "85 1380000:91 1780000:98"
	set_param cpu4 min_sample_time 18000
fi

if [ "$action" = "performance" ]; then
	set_param cpu0 above_hispeed_delay "18000 1680000:58000"
	set_param cpu0 hispeed_freq 1380000
	set_param cpu0 go_hispeed_load 98
	set_param cpu0 boostpulse_duration 18000
	set_param cpu0 target_loads "85 980000:68 1380000:75 1680000:99"
	set_param cpu0 min_sample_time 58000
	set_param cpu4 above_hispeed_delay "18000 1780000:138000"
	set_param cpu4 hispeed_freq 1380000
	set_param cpu4 go_hispeed_load 80
	set_param cpu4 boostpulse_duration 38000
	set_param cpu4 target_loads "85 1380000:64 1780000:98"
	set_param cpu4 min_sample_time 18000
fi

if [ "$action" = "fast" ]; then
	# nothing
fi

chmod 0444 /sys/devices/system/cpu/cpu0/cpufreq/interactive/*
chmod 0444 /sys/devices/system/cpu/cpu4/cpufreq/interactive/*

exit 0